﻿using Microsoft.AspNetCore.Mvc;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YiSha.Admin.Web.Controllers;
using YiSha.Business.RedBookFormManage;
using YiSha.Entity.RedBookFormManage;
using YiSha.Entity.SystemManage;
using YiSha.Model.Param.RedBookFormManage;
using YiSha.Model.Result;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Web.Code;

namespace YiSha.Admin.Web.Areas.RedBookFormManage.Controllers
{
    [Area("RedBookFormManage")]
    public class RedBookFormListController : BaseController
    {
        private RedBookFormBll redBookFormBll = new RedBookFormBll();
        #region 视图功能
        [AuthorizeFilter("redbookform:redbookformlist:view")]
        public async Task<IActionResult> FormIndex()
        {
            OperatorInfo operatorInfo = await Operator.Instance.Current();
            string OperatorFlag = "0";
            if (!string.IsNullOrEmpty(operatorInfo.RoleNames) && operatorInfo.RoleNames.Contains("管理员"))
            {
                OperatorFlag = "1";
            }
            ViewBag.OperatorFlag = OperatorFlag;
            return View();
        }
        public IActionResult RedBookForm()
        {
            return View();
        }
        #endregion


        #region 获取数据
        [HttpGet]
        [AuthorizeFilter("redbookform:redbookformlist:search")]
        public async Task<IActionResult> GetPageListJson(RedBookFormParam param, Pagination pagination)
        {
            TData<List<RedBookFormEntity>> obj = await redBookFormBll.GetPageList(param, pagination);

            return Json(obj);
        }

        [HttpGet]
        [AuthorizeFilter("redbookform:redbookformlist:view")]
        public async Task<IActionResult> GetFormJson(long id)
        {
            TData<RedBookFormEntity> obj = await redBookFormBll.GetEntity(id);
            var ss = Json(obj);
            return Json(obj);
        }
        [HttpGet]
        [AuthorizeFilter("redbookform:redbookformlist:view")]
        public async Task<IActionResult> GetFormAreaJson()
        {
            TData<List<AreaEntity>> obj = await new YiSha.Business.SystemManage.AreaBLL().GetList(new Model.Param.SystemManage.AreaListParam());
            obj.Data = obj.Data.Where(x => x.AreaLevel < 3).ToList();
            TData<List<ZtreeInfo>> ztreeInfo = new TData<List<ZtreeInfo>>();
            ztreeInfo.Data = new List<ZtreeInfo>();
            foreach (AreaEntity area in obj.Data)
            {
                ztreeInfo.Data.Add(new ZtreeInfo
                {
                    id = area.AreaCode.ParseToLong(),
                    pId = area.ParentAreaCode.ParseToLong(),
                    name = area.AreaName
                });
            }
            return Json(ztreeInfo);
        }

        [HttpGet]
        public async Task<IActionResult> GetMaxSortJson()
        {
            TData<int> obj = await redBookFormBll.GetMaxSort();
            return Json(obj);
        }
        [HttpGet]
        [AuthorizeFilter("redbookform:redbookformlist:view")]
        public async Task<IActionResult> GetUserDataJson()
        {

            TData<List<SelectData>> obj = new TData<List<SelectData>>();
            obj.Data = new List<SelectData>();

            obj = await redBookFormBll.GetUserDataList();
            obj.Tag = 1;

            return Json(obj);
        }
        #endregion

        #region 提交数据
        [HttpPost]
        [AuthorizeFilter("redbookform:redbookformlist:add,redbookform:redbookformlist:edit")]
        public async Task<IActionResult> SaveFormJson(RedBookFormEntity entity)
        {
            TData<string> obj = await redBookFormBll.SaveForm(entity);
            return Json(obj);
        }
        [HttpPost]
        [AuthorizeFilter("redbookform:redbookformlist:delete")]
        public async Task<IActionResult> DeleteFormJson(string ids)
        {
            TData obj = await redBookFormBll.DeleteForm(ids);
            return Json(obj);
        }
        #endregion
    }
}
